﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;

using MT.eVotePRS.Device;
using MT.eVotePRS.Model;

using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Logic;

namespace MT.eVotePRS.Presentation.Presenter
{
    public class CheckStatusPresenter
    {
        private IViewCheckStatus view;

        private StudentActions studentAction;
        private HandsetActions handsetAction;
        private InventoryTxnActions txnAction;

        public StudentActions StudentAction
        {
            get { return studentAction; }
            set { studentAction = value; }
        }
        public HandsetActions HandsetAction
        {
            get { return handsetAction; }
            set { handsetAction = value; }
        }
        public InventoryTxnActions TxnAction
        {
            get { return txnAction; }
            set { txnAction = value; }
        }

        public void SetView(IViewCheckStatus view, IDeviceAPIHelper ars)
        {
            this.view = view;
            view.PropertyChanged += OnViewPropertyChanged;
            view.IdEntered += OnViewIdEntered;

        }

        private void OnViewIdEntered(object sender, EventArgs e)
        {
            if (view.ToCheck == CheckingStatus.Student)
            {
                StudentInfo student = studentAction.FindStudentByNo(view.Student.StudentNo);
                InventoryTxn txn = txnAction.CheckStudentStatus(view.Student.StudentNo);
                Handsets handset = new Handsets();
                if (txn != null && txn.Id > 0)
                {
                    handset.MAC = txn.HandsetID;
                }

                view.Student = student;
                view.Txn = txn;
                view.Handset = handset;
            }
            else if (view.ToCheck == CheckingStatus.Handset)
            {
                InventoryTxn txn = txnAction.CheckHandsetStatus(view.Handset.MAC);
                Handsets handset = new Handsets();
                handset.MAC = view.Handset.MAC;

                StudentInfo student = new StudentInfo();

                if (txn != null && txn.Id > 0)
                {
                    student = studentAction.FindStudentByNo(txn.StudentID);
                }

                view.Student = student;
                view.Txn = txn;
                view.Handset = handset;
            }
        }

        private void OnViewPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //if (e.PropertyName == "STUDENT")
            //{
            //    StudentInfo student =  studentAction.FindStudentByNo(view.Student.StudentNo);
            //    InventoryTxn txn = txnAction.CheckStudentStatus(view.Student.StudentNo);
            //    Handsets handset = new Handsets();
            //    if (txn != null && txn.Id > 0)
            //    {
            //        handset.MAC = txn.HandsetID;
            //    }

            //    view.Student = student;
            //    view.Txn = txn;
            //    view.Handset = handset;
            //}
            //else if (e.PropertyName == "HANDSET")
            //{
            //    InventoryTxn txn = txnAction.CheckHandsetStatus(view.Handset.MAC);                
            //    Handsets handset = new Handsets();
            //    handset.MAC = view.Handset.MAC;

            //    StudentInfo student = new StudentInfo();

            //    if (txn != null && txn.Id > 0)
            //    {
            //        student = studentAction.FindStudentByNo(txn.StudentID);
            //    }

            //    view.Student = student;
            //    view.Txn = txn;
            //    view.Handset = handset;
            //}
        }
    }
}
